# 统一设置构建目标输出
set(TARGET_OUT_PATH ${CMAKE_BINARY_DIR}/out CACHE PATH "目标统一输出位置")
# 设置所有可执行文件输出到 build/out/bin/
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "可执行文件输出路径")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TARGET_OUT_PATH}/${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/debug CACHE PATH "调试可执行文件输出路径")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TARGET_OUT_PATH}/${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/release CACHE PATH "发布可执行文件输出路径")

# 设置所有库文件输出到 build/out/lib/
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "库文件输出路径")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${TARGET_OUT_PATH}/${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/debug CACHE PATH "调试库文件输出路径")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${TARGET_OUT_PATH}/${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/release CACHE PATH "发布库文件输出路径")

# 设置所有静态库文件输出到 build/out/lib/
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "静态库文件输出路径")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${TARGET_OUT_PATH}/${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/debug CACHE PATH "调试静态库文件输出路径")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${TARGET_OUT_PATH}/${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/release CACHE PATH "发布静态库文件输出路径")

#  Android 系统配置
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
    # 配置 android 编译选项
    if("${ANDROID_ABI}" STREQUAL "armeabi-v7a")
       set(ARCH "arm")
       set(CPU "armv7-a")
       set(TARGET_ABI "armv7a-linux-androideabi" CACHE STRING "目标 ABI")
       set(CMAKE_C_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang CACHE PATH "gcc 编译器")
       set(CMAKE_CXX_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang++ CACHE PATH "g++ 编译器")
    elseif("${ANDROID_ABI}" STREQUAL "arm64-v8a")
       set(ARCH "aarch64")
       set(CPU "armv8-a")
       set(TARGET_ABI "aarch64-linux-android" CACHE STRING "目标 ABI")
       set(CMAKE_C_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang CACHE PATH "gcc 编译器")
       set(CMAKE_CXX_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang++ CACHE PATH "g++ 编译器")
    elseif("${ANDROID_ABI}" STREQUAL "x86")
       set(ARCH "x86")
       set(CPU "i686" CACHE PATH "架构")
       set(TARGET_ABI "i686-linux-android" CACHE STRING "目标 ABI")
       set(CMAKE_C_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang CACHE PATH "gcc 编译器")
       set(CMAKE_CXX_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang++ CACHE PATH "g++ 编译器")
    elseif("${ANDROID_ABI}" STREQUAL "x86_64")
       set(ARCH "x86_64")
       set(CPU "x86-64")
       set(MARCH ${CPU} CACHE PATH "架构")
       set(TARGET_ABI "x86_64-linux-android" CACHE STRING "目标 ABI")
       set(CMAKE_C_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang CACHE PATH "gcc 编译器")
       set(CMAKE_CXX_COMPILER ${ANDROID_TOOLCHAIN_ROOT}/bin/${TARGET_ABI}${ANDROID_PLATFORM}-clang++ CACHE PATH "g++ 编译器")
    else()
       message(FATAL_ERROR "不支持的 ANDROID_ABI: ${ANDROID_ABI}")
    endif()
    # 配置 android 编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(CMAKE_C_FLAGS "-g3 -O0 -fno-omit-frame-pointer -fPIC -march=${CPU}" CACHE STRING "编译选项")
        set(CMAKE_CXX_FLAGS "-g3 -O0 -fno-omit-frame-pointer -fPIC -march=${CPU}" CACHE STRING "编译选项")
        set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections" CACHE STRING "链接选项")
    elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
        set(CMAKE_C_FLAGS "-fPIC -fdata-sections -ffunction-sections " CACHE STRING "编译选项")
        set(CMAKE_CXX_FLAGS "-fPIC -fdata-sections -ffunction-sections " CACHE STRING "编译选项")
        set(CMAKE_EXE_LINKER_FLAGS "-s -Wl,--gc-sections" CACHE STRING "链接选项")
    endif()
    # 配置 android 工具链
    set(CROSS ${ANDROID_TOOLCHAIN_ROOT}/bin/llvm- CACHE PATH "工具链前缀")
    set(AR ${CROSS}ar CACHE PATH "工具链 ar 程序")
    set(RANLIB ${CROSS}ranlib CACHE PATH "工具链 ranlib 程序")
    set(STRIP ${CROSS}strip CACHE PATH "工具链 strip 程序")
    set(TYPE_DIR android/${CPU}/${ANDROID_PLATFORM} CACHE PATH "类型输出目录")
endif()

#  Linux 配置输出目录
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    # 配置 android 编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(CMAKE_C_FLAGS "-g3 -O0 -fno-omit-frame-pointer -fPIC -march=${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "编译选项")
        set(CMAKE_CXX_FLAGS "-g3 -O0 -fno-omit-frame-pointer -fPIC -march=${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "编译选项")
        set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections" CACHE STRING "链接选项")
    elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
        set(CMAKE_C_FLAGS "-fPIC -fdata-sections -ffunction-sections " CACHE STRING "编译选项")
        set(CMAKE_CXX_FLAGS "-fPIC -fdata-sections -ffunction-sections " CACHE STRING "编译选项")
        set(CMAKE_EXE_LINKER_FLAGS "-s -Wl,--gc-sections" CACHE STRING "链接选项")
    endif()
   set(TYPE_DIR linux/${CMAKE_SYSTEM_VERSION}/${CMAKE_SYSTEM_PROCESSOR} CACHE PATH "类型输出目录")
endif()

# 外部库构建 配置
set(EXTERNA_USERS_SYSTEM_ROOT "/usr" CACHE PATH "用户库构建系统 默认 /usr")
set(SYSTEM_PREFIX ${EXTERNA_USERS_SYSTEM_ROOT}/${TYPE_DIR} CACHE PATH "用户库构建系统 前缀")

# 外部库构建 工程输出路径
set(EXTERNAL_PROJECT_ROOT ${CMAKE_BINARY_DIR} CACHE PATH "外部库构建 工程输出路径")
set(MODULE_SRC ${EXTERNAL_PROJECT_ROOT}/src CACHE PATH "模块 源码目录")
set(MODULE_BUILD ${EXTERNAL_PROJECT_ROOT}/${TYPE_DIR}/build CACHE PATH "模块 构建目录")
set(MODULE_STAMP ${EXTERNAL_PROJECT_ROOT}/${TYPE_DIR}/stamp CACHE PATH "模块 构建记录目录")
set(MODULE_TMP ${EXTERNAL_PROJECT_ROOT}/${TYPE_DIR}/tmp CACHE PATH "模块 构建临时目录")
set(MODULE_INSTALL_DIR ${EXTERNAL_PROJECT_ROOT}/${TYPE_DIR}/install CACHE PATH "模块安装目录")

